package com.it2297.view;

import javax.swing.JPanel;
import javax.swing.JButton;

import com.it2297.controller.NeedyController;
import com.it2297.controller.ProductController;
import com.it2297.model.Needy;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.DefaultComboBoxModel;

public class UpdateNeedyView extends JPanel {
	private final NeedyController controller = new NeedyController(this);
	
	private final JLabel lblNric = new JLabel("NRIC ");
	private final JLabel lblName = new JLabel("Name");
	private final JLabel lblPhone = new JLabel("Phone");
	private final JLabel lblGender = new JLabel("Gender");
	private final JTextField NameTxt = new JTextField();
	private final JTextField PhoneTxt = new JTextField();
	private final JLabel lblAddress = new JLabel("Address");
	private final JTextArea AddressTxtPane = new JTextArea();
	private final JLabel lblEmail = new JLabel("Email");
	private final JTextField EmailTxt = new JTextField();
	private final JLabel lblRace = new JLabel("Race");
	private final JLabel lblNewLabel_5 = new JLabel("Update Needy Resident Personal Record ");
	private final JTextField NricTxt = new JTextField();
	private final JLabel lblDateOfBirth = new JLabel("Date Of Birth");
	private final JLabel lblStatus = new JLabel("Status");
	private final JLabel lblCountryOfBirth = new JLabel("Country Of Birth");
	private final JTextField StatusTxt = new JTextField();
	private final JLabel lblHouseholdIncome = new JLabel("HouseHold Income");
	private final JLabel lblNewLabel_10 = new JLabel("Number Of Dependent");
	private final JLabel lblNewLabel_11 = new JLabel("New label");
	private final JLabel lblNewLabel_12 = new JLabel("Family Size");
	private final JTextField HouseholdIncomeTxt = new JTextField();
	private final JTextField NumOfDependentTxt = new JTextField();
	private final JTextField FamilySizeTxt = new JTextField();
	private final JButton btnUpdate = new JButton("Update");
	private final JButton btnGet = new JButton("Get");
	private final JTextField pwdTxt = new JTextField();
	private final JLabel lblNewLabel = new JLabel("Password");
	private final JComboBox comboBoxRace = new JComboBox();
	private final JComboBox comboBoxGender = new JComboBox();
	private final JComboBox comboBoxCountry = new JComboBox();
	private final JTextField DateTxt = new JTextField();
	private final JButton btnRefresh = new JButton("Refresh");

	/**
	 * Create the panel.
	 */
	public UpdateNeedyView() {
		DateTxt.setEditable(false);
		DateTxt.setBounds(309, 358, 172, 23);
		DateTxt.setColumns(10);
		pwdTxt.setBounds(309, 221, 172, 23);
		pwdTxt.setColumns(10);
		FamilySizeTxt.setBounds(309, 516, 172, 23);
		FamilySizeTxt.setColumns(10);
		NumOfDependentTxt.setBounds(309, 481, 172, 24);
		NumOfDependentTxt.setColumns(10);
		HouseholdIncomeTxt.setBounds(309, 453, 172, 20);
		HouseholdIncomeTxt.setColumns(10);
		StatusTxt.setEditable(false);
		StatusTxt.setBounds(309, 389, 172, 23);
		StatusTxt.setColumns(10);
		NricTxt.setBounds(309, 36, 172, 26);
		NricTxt.setColumns(10);
		EmailTxt.setBounds(309, 286, 172, 30);
		EmailTxt.setColumns(10);
		PhoneTxt.setBounds(309, 178, 172, 32);
		PhoneTxt.setColumns(10);
		setLayout(null);
		NameTxt.setBounds(309, 73, 172, 32);
		
		NameTxt.setColumns(10);
		
		this.setSize(1000,700);

		initGUI();
	}
	
	
	public String getNeedyNric(){
		return NricTxt.getText();
		}
		
		public String getNeedyName(){
			return NameTxt.getText();
		}
		
		public void setNeedyName(String name){
			NameTxt.setText(name);
		}
		
		public String getNeedyAddress(){
			return AddressTxtPane.getText();
		}
		
		public void setNeedyAddress(String address){
			AddressTxtPane.setText(address);
		}
		
		public String getNeedyPhone(){
			return PhoneTxt.getText();
		}
		
		public void setNeedyPhone(String phone){
		       PhoneTxt.setText(phone);
		}
		
		public String getNeedyPassword(){
			return pwdTxt.getText();
			}
		
	    public void setNeedyPassword(String password){
			       pwdTxt.setText(password);
			}
			
		
		public String getNeedyGender(){
			return (String) comboBoxGender.getSelectedItem();
		}
		
		public void setNeedyGender(String gender){
			comboBoxGender.setSelectedItem(gender);
		}
		
		
		public String getNeedyEmail(){
			return EmailTxt.getText();
		}
		
		public void setNeedyEmail(String email){
			EmailTxt.setText(email);
		}
		
		
		public String getNeedyEthnic(){
			return (String) comboBoxRace.getSelectedItem();
		}
		
		public void setNeedyEthnic(String ethnic){
			comboBoxRace.setSelectedItem(ethnic);
		}
		
	//dob
		
		 public Date getNeedyDob(){
			 DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); 
			 
			 Date date = null;
			try {
				date = (Date)formatter.parse(DateTxt.getText());
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println(date);
			 return date;
			 
		  }

		
		 public void setNeedyDob(Date dob){
			 if(!(dob == null)){
			 DateFormat df = new SimpleDateFormat("yyyy-MM-dd ");
			 String reportDate = df.format(dob);
			 DateTxt.setText(reportDate);
			 }
		 }
		 
		
		
		
		
		public String getNeedyStatus(){
			return StatusTxt.getText();
		}
		
		public void setNeedyStatus(String status){
			StatusTxt.setText(status);
		}
		
		public String getNeedyCountryOfBirth(){
			return (String) comboBoxCountry.getSelectedItem();
		}
		
		public void setNeedyCountryOfBirth(String country){
			comboBoxCountry.setSelectedItem(country);
		}
		
		public double getNeedyHouseHoldIncome(){
			return Double.parseDouble(HouseholdIncomeTxt.getText());
		}
		
		//needy 
		
		public void setNeedyHouseHoldIncome(double householdincome){
			HouseholdIncomeTxt.setText(Double.toString(householdincome));
		}
		
		public int getNeedyNumOfDependent(){
			return Integer.parseInt(NumOfDependentTxt.getText()) ;
		}
		
		public void setNeedyNumOfIndependent(int numofindependent){
			NumOfDependentTxt.setText(Integer.toString(numofindependent));
		}
		
		public int getNeedyFamilySize(){
			return Integer.parseInt(FamilySizeTxt.getText());
		}
		
		public void setNeedyFamilySize(int familysize){
			FamilySizeTxt.setText(Integer.toString(familysize));
		}
	
	
		
	
	
	private void initGUI() {
		setLayout(null);
		lblNric.setFont(new Font("Arial", Font.PLAIN, 19));
		lblNric.setBounds(221, 32, 57, 23);
		
		add(lblNric);
		lblName.setFont(new Font("Arial", Font.PLAIN, 19));
		lblName.setBounds(221, 75, 51, 23);
		
		add(lblName);
		
		add(NameTxt);
		lblPhone.setFont(new Font("Arial", Font.PLAIN, 19));
		lblPhone.setBounds(209, 172, 69, 38);
		
		add(lblPhone);
		lblGender.setFont(new Font("Arial", Font.PLAIN, 19));
		lblGender.setBounds(195, 246, 83, 32);
		
		add(lblGender);
		
		add(PhoneTxt);
		lblAddress.setFont(new Font("Arial", Font.PLAIN, 19));
		lblAddress.setBounds(206, 114, 72, 38);
		
		add(lblAddress);
		AddressTxtPane.setWrapStyleWord(true);
		AddressTxtPane.setLineWrap(true);
		AddressTxtPane.setBounds(309, 114, 172, 56);
		
		add(AddressTxtPane);
		lblEmail.setFont(new Font("Arial", Font.PLAIN, 19));
		lblEmail.setBounds(195, 279, 69, 38);
		
		add(lblEmail);
		
		add(EmailTxt);
		lblRace.setFont(new Font("Arial", Font.PLAIN, 19));
		lblRace.setBounds(195, 318, 69, 32);
		
		add(lblRace);
		lblNewLabel_5.setFont(new Font("Arial", Font.PLAIN, 19));
		lblNewLabel_5.setBounds(222, 0, 355, 23);
		
		add(lblNewLabel_5);
		
		add(NricTxt);
		lblDateOfBirth.setFont(new Font("Arial", Font.PLAIN, 19));
		lblDateOfBirth.setBounds(177, 354, 115, 23);
		
		add(lblDateOfBirth);
		lblStatus.setFont(new Font("Arial", Font.PLAIN, 19));
		lblStatus.setBounds(221, 388, 58, 24);
		
		add(lblStatus);
		lblCountryOfBirth.setFont(new Font("Arial", Font.PLAIN, 19));
		lblCountryOfBirth.setBounds(145, 423, 147, 23);
		
		add(lblCountryOfBirth);
		
		add(StatusTxt);
		lblHouseholdIncome.setFont(new Font("Arial", Font.PLAIN, 19));
		lblHouseholdIncome.setBounds(114, 449, 164, 23);
		
		add(lblHouseholdIncome);
		lblNewLabel_10.setFont(new Font("Arial", Font.PLAIN, 19));
		lblNewLabel_10.setBounds(98, 479, 204, 23);
		
		add(lblNewLabel_10);
		lblNewLabel_11.setBounds(118, 705, 46, 14);
		
		add(lblNewLabel_11);
		lblNewLabel_12.setFont(new Font("Arial", Font.PLAIN, 19));
		lblNewLabel_12.setBounds(163, 513, 115, 23);
		
		add(lblNewLabel_12);
		
		add(HouseholdIncomeTxt);
		
		add(NumOfDependentTxt);
		
		add(FamilySizeTxt);
		
		btnUpdate.setBounds(195, 547, 122, 32);
		
		add(btnUpdate);
		
		btnGet.setBounds(509, 33, 92, 32);
		
		add(btnGet);
		
		btnUpdate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
		
				updateNeedy();
		
			}
		});
		
		btnGet.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {			
				String nric = NricTxt.getText();
				
				
				controller.getNeedy(nric);
				NricTxt.setEditable(false);
		
				if(NameTxt.getText().isEmpty()){
					
					JOptionPane.showMessageDialog(null,"Needy resident does not exist!");
					NricTxt.setText("");
					NricTxt.setEditable(true);
					HouseholdIncomeTxt.setText("");
					NumOfDependentTxt.setText("");
					FamilySizeTxt.setText("");
					pwdTxt.setText("");
				}
				else 
				{
					JOptionPane.showMessageDialog(null,"Details Retrieved");
				}
				
				
				
			}
		});
		
		
		add(pwdTxt);
		lblNewLabel.setFont(new Font("Arial", Font.PLAIN, 19));
		lblNewLabel.setBounds(189, 221, 89, 23);
		
		add(lblNewLabel);
		comboBoxRace.setEditable(true);
		
		
		comboBoxRace.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			    comboBoxRace.getSelectedItem();
				System.out.println(getNeedyEthnic());
			}
		});
		
		
		
		comboBoxRace.setModel(new DefaultComboBoxModel(new String[] {"CHI", "IND", "MAL"}));
		comboBoxRace.setBounds(309, 327, 172, 20);
		
		add(comboBoxRace);
		comboBoxGender.setEditable(true);
		
		comboBoxGender.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				comboBoxGender.getSelectedItem();
				System.out.println(getNeedyGender());
			}
		});
		
		comboBoxGender.setModel(new DefaultComboBoxModel(new String[] {"Female", "Male", "Others"}));
		comboBoxGender.setBounds(309, 255, 172, 20);
		
		add(comboBoxGender);
		comboBoxCountry.setEditable(true);
		comboBoxCountry.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				comboBoxCountry.getSelectedItem();
				System.out.println(getNeedyCountryOfBirth());
			}
		});
		comboBoxCountry.setModel(new DefaultComboBoxModel(new String[] {"SGP", "MYS", "IND", "THA"}));
		comboBoxCountry.setBounds(309, 423, 172, 20);
		
		add(comboBoxCountry);
		
		add(DateTxt);
		btnRefresh.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				NricTxt.setEditable(true);
				
				NricTxt.setText("");
				NameTxt.setText("");
				AddressTxtPane.setText("");
				PhoneTxt.setText("");
				pwdTxt.setText("");
				EmailTxt.setText("");
				DateTxt.setText("");
				StatusTxt.setText("");
				HouseholdIncomeTxt.setText("");
				NumOfDependentTxt.setText("");
				FamilySizeTxt.setText("");
				
				
				
			}
		});
		btnRefresh.setBounds(364, 550, 117, 29);
		
		add(btnRefresh);
	}
	
	private void updateNeedy(){
		controller.UpdateNeedy();
	}
}
